package com.mhkj.common.util;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

import com.mhkj.base.functionOffice.entity.FunctionOffice;
import com.mhkj.base.functionOffice.service.FunctionOfficeService;
import com.mhkj.base.itemInfo.item.entity.Item;
import com.mhkj.base.itemInfo.item.service.ItemService;
import com.mhkj.base.rbac.role.entity.SystemRole;
import com.mhkj.base.rbac.user.entity.SystemUser;
import com.mhkj.base.rbac.user.service.SystemUserService;
import com.mhkj.common.constant.Constants;

/**
 * 项目帮助类
 * @author mics
 * @date 2018年9月2日
 * @version 1.0
 */
@Component
public class ItemHelper {
	@Resource
	private SystemUserService systemUserService;
	@Resource
	private ItemService itemService;
	@Resource
	private FunctionOfficeService functionOfficeService;
	
	
	public  List<Item> getCurrentUserItems(){
		Subject currentUser = SecurityUtils.getSubject();  
        SystemUser user = (SystemUser) currentUser.getSession().getAttribute(Constants.CURRENT_SYSUSER);
        List<Item> items = new ArrayList<Item>();
        if(user.getLoginName().equals(Constants.ROOT_NAME)){
			items = itemService.findAll();
        }else{
           SystemUser systemUser = systemUserService.getById(user.getId());
    	   List<SystemRole> systemRoles = systemUser.getSystemRoles();
           for(SystemRole systemRole:systemRoles){
           	 items.addAll(systemRole.getItems());
           }
        }
		return items;
	}
	
	public  List<FunctionOffice> getCurrentUserFunctionOffices(){
		Subject currentUser = SecurityUtils.getSubject();  
        SystemUser user = (SystemUser) currentUser.getSession().getAttribute(Constants.CURRENT_SYSUSER);
        List<FunctionOffice> functionOffices = new ArrayList<FunctionOffice>();
        if(user.getLoginName().equals(Constants.ROOT_NAME)){
        	functionOffices = functionOfficeService.findAll();
        }else{
            SystemUser systemUser = systemUserService.getById(user.getId());
    	   List<SystemRole> systemRoles = systemUser.getSystemRoles();
           for(SystemRole systemRole:systemRoles){
        	   functionOffices.addAll(systemRole.getFunctions());
           }
        }
		return functionOffices;
	}
	
}
